package com.rickpan.repository;

import com.rickpan.entity.TeamActivityLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 团队活动日志数据访问层
 * 
 * @author RickPan Team
 * @since 2024-01-15
 */
@Repository
public interface TeamActivityLogRepository extends JpaRepository<TeamActivityLog, Long> {

    /**
     * 根据团队ID查找活动日志列表
     */
    List<TeamActivityLog> findByTeamIdOrderByCreatedAtDesc(Long teamId);

    /**
     * 根据团队ID查找活动日志列表（分页）
     */
    Page<TeamActivityLog> findByTeamIdOrderByCreatedAtDesc(Long teamId, Pageable pageable);

    /**
     * 根据用户ID查找活动日志列表
     */
    List<TeamActivityLog> findByUserIdOrderByCreatedAtDesc(Long userId);

    /**
     * 根据用户ID查找活动日志列表（分页）
     */
    Page<TeamActivityLog> findByUserIdOrderByCreatedAtDesc(Long userId, Pageable pageable);

    /**
     * 根据团队ID和用户ID查找活动日志列表
     */
    List<TeamActivityLog> findByTeamIdAndUserIdOrderByCreatedAtDesc(Long teamId, Long userId);

    /**
     * 根据团队ID和用户ID查找活动日志列表（分页）
     */
    Page<TeamActivityLog> findByTeamIdAndUserIdOrderByCreatedAtDesc(Long teamId, Long userId, Pageable pageable);

    /**
     * 根据团队ID和操作类型查找活动日志列表
     */
    List<TeamActivityLog> findByTeamIdAndActionTypeOrderByCreatedAtDesc(Long teamId, TeamActivityLog.ActionType actionType);

    /**
     * 根据团队ID和操作类型查找活动日志列表（分页）
     */
    Page<TeamActivityLog> findByTeamIdAndActionTypeOrderByCreatedAtDesc(Long teamId, TeamActivityLog.ActionType actionType, Pageable pageable);

    /**
     * 根据团队ID和目标类型查找活动日志列表
     */
    List<TeamActivityLog> findByTeamIdAndTargetTypeOrderByCreatedAtDesc(Long teamId, TeamActivityLog.TargetType targetType);

    /**
     * 根据团队ID和目标类型查找活动日志列表（分页）
     */
    Page<TeamActivityLog> findByTeamIdAndTargetTypeOrderByCreatedAtDesc(Long teamId, TeamActivityLog.TargetType targetType, Pageable pageable);

    /**
     * 根据团队ID和目标ID查找活动日志列表
     */
    List<TeamActivityLog> findByTeamIdAndTargetIdOrderByCreatedAtDesc(Long teamId, Long targetId);

    /**
     * 根据团队ID和目标类型和目标ID查找活动日志列表
     */
    List<TeamActivityLog> findByTeamIdAndTargetTypeAndTargetIdOrderByCreatedAtDesc(Long teamId, TeamActivityLog.TargetType targetType, Long targetId);

    /**
     * 根据时间范围查找活动日志
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND l.createdAt BETWEEN :startDate AND :endDate " +
           "ORDER BY l.createdAt DESC")
    List<TeamActivityLog> findByTeamIdAndDateRange(@Param("teamId") Long teamId,
                                                 @Param("startDate") LocalDateTime startDate,
                                                 @Param("endDate") LocalDateTime endDate);

    /**
     * 根据时间范围查找活动日志（分页）
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND l.createdAt BETWEEN :startDate AND :endDate " +
           "ORDER BY l.createdAt DESC")
    Page<TeamActivityLog> findByTeamIdAndDateRange(@Param("teamId") Long teamId,
                                                 @Param("startDate") LocalDateTime startDate,
                                                 @Param("endDate") LocalDateTime endDate,
                                                 Pageable pageable);

    /**
     * 查找今日活动日志
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND DATE(l.createdAt) = CURRENT_DATE " +
           "ORDER BY l.createdAt DESC")
    List<TeamActivityLog> findTodayActivitiesByTeam(@Param("teamId") Long teamId);

    /**
     * 查找本周活动日志
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND l.createdAt >= :weekStart " +
           "ORDER BY l.createdAt DESC")
    List<TeamActivityLog> findWeekActivitiesByTeam(@Param("teamId") Long teamId,
                                                 @Param("weekStart") LocalDateTime weekStart);

    /**
     * 查找本月活动日志
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND l.createdAt >= :monthStart " +
           "ORDER BY l.createdAt DESC")
    List<TeamActivityLog> findMonthActivitiesByTeam(@Param("teamId") Long teamId,
                                                  @Param("monthStart") LocalDateTime monthStart);

    /**
     * 搜索活动日志
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND (l.targetTitle LIKE %:keyword% OR l.description LIKE %:keyword%) " +
           "ORDER BY l.createdAt DESC")
    List<TeamActivityLog> searchActivitiesByTeam(@Param("teamId") Long teamId, 
                                               @Param("keyword") String keyword);

    /**
     * 搜索活动日志（分页）
     */
    @Query("SELECT l FROM TeamActivityLog l WHERE l.teamId = :teamId " +
           "AND (l.targetTitle LIKE %:keyword% OR l.description LIKE %:keyword%) " +
           "ORDER BY l.createdAt DESC")
    Page<TeamActivityLog> searchActivitiesByTeam(@Param("teamId") Long teamId, 
                                               @Param("keyword") String keyword, 
                                               Pageable pageable);

    /**
     * 根据团队ID统计活动日志数量
     */
    long countByTeamId(Long teamId);

    /**
     * 根据团队ID和操作类型统计活动日志数量
     */
    long countByTeamIdAndActionType(Long teamId, TeamActivityLog.ActionType actionType);

    /**
     * 根据团队ID和目标类型统计活动日志数量
     */
    long countByTeamIdAndTargetType(Long teamId, TeamActivityLog.TargetType targetType);

    /**
     * 获取团队活动类型统计
     */
    @Query("SELECT l.actionType as actionType, COUNT(l) as count FROM TeamActivityLog l " +
           "WHERE l.teamId = :teamId GROUP BY l.actionType")
    List<ActivityTypeCount> getActivityTypeStatsByTeam(@Param("teamId") Long teamId);

    /**
     * 获取团队目标类型统计
     */
    @Query("SELECT l.targetType as targetType, COUNT(l) as count FROM TeamActivityLog l " +
           "WHERE l.teamId = :teamId GROUP BY l.targetType")
    List<TargetTypeCount> getTargetTypeStatsByTeam(@Param("teamId") Long teamId);

    /**
     * 获取团队用户活动统计
     */
    @Query("SELECT l.userId as userId, COUNT(l) as count FROM TeamActivityLog l " +
           "WHERE l.teamId = :teamId GROUP BY l.userId ORDER BY COUNT(l) DESC")
    List<UserActivityCount> getUserActivityStatsByTeam(@Param("teamId") Long teamId);

    /**
     * 活动类型统计接口
     */
    interface ActivityTypeCount {
        TeamActivityLog.ActionType getActionType();
        Long getCount();
    }

    /**
     * 目标类型统计接口
     */
    interface TargetTypeCount {
        TeamActivityLog.TargetType getTargetType();
        Long getCount();
    }

    /**
     * 用户活动统计接口
     */
    interface UserActivityCount {
        Long getUserId();
        Long getCount();
    }
}
